//
//  Status.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/20.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#import "Status.h"
#import "User.h"

@interface Status ()

@end

@implementation Status

- (NSArray *)imageURLs {
    NSArray *urls = self.pic_urls;
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    NSDictionary * item = [[NSDictionary alloc] init];
    for (item in urls) {
        //相信后端给的urlString 一定能够转换成 url
        NSURL *url = [NSURL URLWithString:item[@"thumbnail_pic"]];
        [tempArray addObject:url];
    }
    _imageURLs = tempArray;
    return _imageURLs;
}

//定义计算型属性  同时代表 原创微博和转发微博的图片数组
- (NSArray *)pictureURLs {
    //如果转发微博不为空
    if (self.retweeted_status != nil) {
        return self.retweeted_status.imageURLs;
    }
    //如果是原创微博
    return self.imageURLs;
}

// KVC 设置初始值
- (instancetype)initWithDict: (NSDictionary *)dict {
    self = [super init];
    [self setValuesForKeysWithDictionary:dict];
    return self;
}

- (void)setValue:(id)value forKey:(NSString *)key {
    if ([key isEqual: @"user"]) {
        // 对 user 属性初始化
        NSDictionary *dict = [[NSDictionary alloc] init];
        if ([dict isKindOfClass:[NSDictionary class]] ) {
            dict = value;
            self.user = [[User alloc] initWithDict:dict];
        }
        return;
    }
    if ([key isEqual: @"retweeted_status"]) {
        // 对 user 属性初始化
        NSDictionary *dict = [[NSDictionary alloc] init];
        if ([dict isKindOfClass:[NSDictionary class]] ) {
            dict = value;
            self.retweeted_status = [[Status alloc] initWithDict:dict];
        }
        return;
    }
    [super setValue:value forKey:key];
}


//过滤掉没有用到的字段
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
}
@end
